import sys
import gen_view_info
from optparse import OptionParser
from  mako.template import Template
	
def main(para):
    makotext = '''
<zone_config nextfile = "">
% if len(viewlist) != 0:
    % for view in viewlist:
    <view name = \"${view}\" >
        % if viewlist.index(view) == 0:
        <tsig_key name = \"key1\" />
        % endif
        <acl file = \"${para_dic["aclpath"]}/${view}_acl.xml\" />
    </view>
    % endfor
% endif
</zone_config>
'''
    if 'viewnum' not in para:
        para['viewnum'] = 0
    if 'viewprefix' not in para:
        para['viewprefix'] = 'view'
    viewlist = gen_view_info.gen_rand_view(para['viewnum'], para['viewprefix'])
    tpl = Template(makotext)
    txt = tpl.render(viewlist=viewlist, para_dic=para)

    with open(para['outfile'], "w") as fp:
        fp.write(txt)

def getopt():
    """
    get options from user.
    """
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-c", "--acl", dest="aclpath", help="specify acl file path ")
    parser.add_option("-v", "--viewnum", dest="viewnum", default = 0, type = int,  help="specify viewnum ")
    parser.add_option("-V", "--viewprefix", dest="viewprefix", default='view', help="specify view name ")
    parser.add_option("-o", "--outfile", dest="outfile", help="specify output file name ")
    (options, args) = parser.parse_args()
    if(options.outfile == None):
    	parser.print_help()
    	sys.exit(1)
    
    return options
   
if __name__ == "__main__":
    para = getopt()
    main(vars(para))
    
